package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UserController {

    private final String USER_KEY = "user";

    @RequestMapping("/login")
    public boolean login(HttpSession httpSession){
        //伪代码 省去验证过程
        User user = new User();
        user.setId(209000804);
        user.setName("tanchen");
        user.setPassword(441845471);
        httpSession.setAttribute(USER_KEY,user);
        return true;
    }

    @RequestMapping("/getsession")
    public User getsession(HttpServletRequest request){
        HttpSession session = request.getSession(false);

        if (session != null){
            return (User) session.getAttribute(USER_KEY);
        }
        return null;
    }
}
